<?php namespace Admin\Controller; 

use Admin\Model\Goods;

class GoodsController extends AuthController{

	protected $db;
	//构造函数
	public function __init()
	{
		$this->db = new Goods;
	}
	
    //显示商品列表
    public function index(){
    	$data = $this->db->getAll(Q('get.cat_id'));
		//p($data);die;
      	 View::with('data',$data)->make();
    }
	
	//添加商品
	public function add()
	{
		
		if(IS_POST){
			if($this->db->store()){
				View::success('添加商品成功',U('index'));
			}else{
				View::error($this->db->getError());
			}
		}else{
			//分配栏目
			$cat = new \Admin\Model\Cat;
			$catData = $cat->getAll();
			//分配品牌数据
			$brand = new \Admin\Model\Brand;
			$brandData = $brand->getAll();
			//p($brandData);die;
			//分配类型
			$type = new \Admin\Model\ShopType;
			$ShpoType = $type->getAll();
			
			View::with('catData',$catData)->with('brandData',$brandData)->with('ShpoType',$ShpoType)->make();
		}	
		
	}
	
	
	//修改商品
	public function edit()
	{
		if(IS_POST){
			if($this->db->edit()){
				View::success('修改商品成功',U('index'));
			}else{
				View::error($this->db->getError());
			}
		}else{
			
			//分配栏目
			$cat = new \Admin\Model\Cat;
			$catData = $cat->getAll();
			//分配品牌数据
			$brand = new \Admin\Model\Brand;
			$brandData = $brand->getAll();
			//分配类型
			$type = new \Admin\Model\ShopType;
			$ShpoType = $type->getAll();
			//读取图集数据
			$pics = Db::table('shop_pics')->where('goods_id',$_GET['goods_id'])->get();
			
			//获得指定id的商品数据
			$file = $this->db->one($_GET['goods_id']);
			View::with('file',$file)->with('brandData',$brandData)->with('ShpoType',$ShpoType)->with('catData',$catData)->with('pics',$pics)->make();
		}
		
	}
	
	//获得属性列表
	public function getAttrList()
	{
		echo $this->db->getAttrList($_GET['type_id']);
		exit;
	}
	
	
	//删除列表
	public function del()
	{
		if($this->db->del($_GET['id'])){
			View::success('删除成功','U(index)');
		}else{
			View::error($this->db->getError());
		}
	}
	
	
}
